package deal

import (
	"strings"
	"week13/errors"
	"week13/internal/data"
	"week13/pkg/orm"
)

const TABLENAME = "deal"
type DealDO struct {
	DealId int64 `json:"dealId" xorm:"pk autoincr BITINT dealid"`
	AgentId int64 `json:"agentid" xorm:"BIGINT agentid"`
	ItemId int64 `json:"itemid" xorm:"INT itemid"`
	Price int `json:"price" xorm:"INT price"`
	Type int `json:"type" xorm:"INT type"`
	State int `json:"state" xorm:"INT state"`
	RefundState int `json:"refundState" xorm:"INT refund_state"`
	PayState int `json:"payState" xorm:"INT pay_state"`
	GenTime int `json:"genTime" xorm:"INT gen_time"`
	PayTime int `json:"payTime" xorm:"INT pay_time"`
	RefundTime int `json:"refundTime" xorm:"INT refund_time"`
	Addtime int `json:"addtime" xorm:"INT created addtime"`
	Mtime string `json:"mtime" xorm:"TIMESTAMP mtime updated"`
	FromEndPoint int `json:"fromEndPoint" xorm:"INT  from_end_point"`
	PayType int `json:"payType" xorm:"INT pay_type"`
}
type DealWhere struct {
	DealId []interface{} `column:"dealid"`
	AgentId []interface{} `column:"agentid"`
	ItemId []interface{} `column:"itemid"`
	Type []interface{} `column:"type"`
	State []interface{} `column:"state"`
	RefundState []interface{} `column:"refund_state"`
	PayState []interface{} `column:"pay_state"`
	GenTime []interface{} `column:"gen_time"`
	PayTime []interface{} `column:"pay_time"`
	RefundTime []interface{} `column:"refund_time"`
	Addtime []interface{} `column:"addtime"`
	PayType []interface{} `column:"pay_type""`
}
func(deal DealDO) TableName() string{
	return TABLENAME
}
//数据访问接口
type DealRepository interface {
	Find(dealId int64) (DealDO,error)
	Save(deal DealDO) (int64,error)
	FindOneByFilter(Do DealDO) (DealDO,error)
	ListByWhere(where orm.Where,page int,pageSize int) ([]DealDO,error)
	CountByWhere(where orm.Where)(int64,error)
}

type DealDao struct {
}
//根据Where查找数据
func (dao *DealDao) ListByWhere(where orm.Where,page int,pageSize int) ([]DealDO,error){
	db,err := data.DBEngin()
	if err != nil {
		return nil,errors.NewConnectFail()
	}
	whereStr, vales := where.GetWhereSqlAndValue()
	incomes := make([]DealDO,0)
	if page>=1 {
		db.Where(whereStr, vales...).Limit(pageSize,(page-1)*pageSize).Find(&incomes)
	} else {
		db.Where(whereStr, vales...).Find(&incomes)
	}
	return incomes,nil
}
//根据Where计算总数
func (dao *DealDao) CountByWhere(where orm.Where)(int64,error){
	db,err := data.DBEngin()
	if err != nil {
		return 0,errors.NewConnectFail()
	}
	whereStr, vales := where.GetWhereSqlAndValue()
	do2 := new(DealDO)
	total, err := db.Where(whereStr, vales...).Count(do2)
	if err != nil {
		return 0,errors.NewExecuteFail(err.Error())
	}
	return total,nil
}
//查找订单信息
func (dao *DealDao) Find(dealId int64) (DealDO,error)  {
	db,err := data.DBEngin()
	if err != nil {
		return DealDO{},errors.NewConnectFail()
	}
	dealDO := DealDO{}
	ret,err := db.ID(dealId).Get(&dealDO)
	if !ret {
		return dealDO, errors.NewNoFound()
	}
	if err != nil {
		return dealDO,errors.NewExecuteFail(err.Error())
	}
	return dealDO,nil
}
//查找订单
func (dao *DealDao) FindOneByFilter(Do DealDO) (DealDO,error)  {
	db,err := data.DBEngin()
	if err != nil {
		return DealDO{},errors.NewConnectFail()
	}
	where := make([]string,0)
	values := make([]interface{},0)
	if Do.DealId >0 {
		where = append(where,"dealid=?")
		values = append(values,Do.DealId)
	}
	if Do.AgentId >0 {
		where  = append(where,"agentid=?")
		values = append(values,Do.AgentId)
	}
	if len(where)<1{
		return DealDO{},errors.NewIllegalParam("Find One Deal  lack where params")
	}
	whereStr := strings.Join(where," AND ")
	data := DealDO{}
	ret,err := db.Where(whereStr,values...).Get(&data)
	if !ret {
		return data, errors.NewNoFound()
	}
	if err != nil {
		return data,errors.NewExecuteFail(err.Error())
	}
	return data,nil
}
//保存订单
func (dao *DealDao) Save(do DealDO) (int64, error)   {
	db,err := data.DBEngin()
	if err != nil {
		return 0,errors.NewConnectFail()
	}
	var affected int64=0
	if do.DealId == 0 {
		affected,err = db.Insert(&do)
		if err !=  nil {
			return affected, errors.NewExecuteFail(err.Error())
		}
		if affected ==0 {
			return affected,errors.NewExecuteFail("INSERT ERROR")
		}
		return do.DealId,nil
	}
	DealId := do.DealId
	do.DealId = 0
	affected, err = db.ID(DealId).Update(do)
	if err !=  nil {
		return affected, errors.NewExecuteFail(err.Error())
	}
	if affected == 0 {
		return affected, errors.NewNothingChange()
	}
	return DealId,nil
}

